package com.zhao.processor;

import com.zhao.service.UserService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import java.lang.reflect.Proxy;
@Component
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class MockAopBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("[DEBUG] Processing bean: " + beanName +
                " of type: " + bean.getClass().getName());

        // 更可靠的判断条件
        if (bean.getClass().getPackage().getName().equals("com.zhao.service")) {
            System.out.println("[DEBUG] Creating proxy for: " + beanName);
            return Proxy.newProxyInstance(
                    bean.getClass().getClassLoader(),
                    bean.getClass().getInterfaces(),
                    (proxy, method, args) -> {
                        System.out.println("[PROXY] Before method: " + method.getName());
                        Object result = method.invoke(bean, args);
                        System.out.println("[PROXY] After method: " + method.getName());
                        return result;
                    });
        }
        return bean;
    }
}
